#!/bin/bash
set -e

export inner_build_rtfs_fname=003.inner_build_rtfs.sh

if [ "x${1}" = "xsubbuild" ]; then
	#如果是上层调用该脚本
	if [ $# -eq 1 ]; then
		#功能码999表示查看帮助
		export fun_code=999
	elif [ $# -eq 2 ]; then
		#获取功能码
		export fun_code=$2
	else
    	echo -e "\033[47;41m子模块参数错误!\033[0m"
    	echo -e "\033[47;41m子模块参数参考: $0 subbuild 调用码 功能码\033[0m"
		exit 1
	fi
    echo -e "\033[30;43m继承环境变量!\033[0m"
else
    echo -e "\033[30;43m配置环境变量!\033[0m"
	#>>-----------------------------------------------------------------------------------
	#切换目录
	cd ../../
	#-------------------------------------------------------------------------------------

	#>>-----------------------------------------------------------------------------------
	#顶层项目目录
	export PROJECT_TOPDIR=$PWD
	#顶层当前配置文件
	export CURCFG_FILE=${PROJECT_TOPDIR}/.curconfig.sh
	#-------------------------------------------------------------------------------------

	#>>env--------------------------------------------------------------------------------
	#设置颜色函数
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.000.colorsetup.sh > /dev/null 
	#导入公共变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.001.envsetup_common.sh > /dev/null 
	#导入平台变量
	source ${PROJECT_TOPDIR}/a.OEMKIT/scripts/dugi.002.envsetup_platform.sh > /dev/null 
	#-------------------------------------------------------------------------------------

	#检查当前机型配置
	if [ ! -f ${CURCFG_FILE} ]; then
		serro "检查当前配置文件... [失败]"
		exit 1
	fi
	source ${CURCFG_FILE} > /dev/null 
	
	#检测当前环境
	check_env

	#>>-----------------------------------------------------------------------------------
	#切回目录
	cd -> /dev/null
	#-------------------------------------------------------------------------------------

	if [ $# -eq 1 ]; then
		export fun_code=$1
	else
		export fun_code=999
	fi
fi

#刷新环境参数
update_copy_env
update_kern_env
update_rtfs_env
update_knfs_env
update_rtfs_env

echo -e "\033[47;42m$0:当前功能码: ${fun_code}\033[0m"

#-----------------------------------------------------------------------
#模块名称
export MODNAME=${OEMDEV_RTFS_CFG_PREFIX}
#压缩包根目录
export MODPKG_ROOT=${SYSKIT_RTFS_PKGDIR_ROOT}
#源码包根目录
export MODSRC_ROOT=${SYSKIT_RTFS_RAWDIR_ROOT}
#模块压缩包文件名称
export MODPKG_NAME=${MODNAME}.tar.gz 
#模块源代码目录名称
export MODSRC_NAME=${MODNAME}
#模块压缩包路径
export MODPKG_PATH=${MODPKG_ROOT}/${MODPKG_NAME}
#模块源代码路径
export MODSRC_PATH=${MODSRC_ROOT}/${MODSRC_NAME}
#模块编译输出根目录
export MODOUT_ROOT=${SYSKIT_RTFS_OUTDIR_ROOT}/${SYSKIT_RTFS_OUT_NAME}
#模块拷贝输出根目录
export MODCPY_ROOT=${IMGBINS_TOPDIR}/${OEMDEV_CUR_CPUCODE}_${OEMDEV_CUR_APPAREA}_${OEMDEV_CUR_TYPE}

#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
#导入公共函数
source 000.import.commonfunc.sh
#-----------------------------------------------------------------------

#-----------------------------------------------------------------------
showusage()
{
	echo "使用说明:"
	echo "$0 [功能码]"
	echo "$0  1       构建${MODNAME}烧写文件"
	echo "$0  999     查看本帮助"
}
#-----------------------------------------------------------------------
check_ubitools()
{
	ubinize -V >/dev/null  2>&1
	if [ ! $? = 0 ]; then
		pwarn "没有安装\"mtd-utils\"工具，开始尝试安装 ..."
		sudo apt-get install mtd-utils
		if [ $? = 0 ];then
			pdone "尝试安装\"mtd-utils\"工具 [完成]"
		else
			perro "尝试安装\"mtd-utils\"工具 [失败]"
			exit 1
		fi
	fi
}
#-----------------------------------------------------------------------
rtfs_koinstall()
{
	local inner_build_rtfs_func=rtfs_koinstall
	snote "${inner_build_rtfs_fname}:${inner_build_rtfs_func}:+++++++++++++++++++++"
	if [ ! -d ${SYSKIT_KERN_OUT_PATH}/modules.install ]; then
		serro "未检测到内核驱动目录:${SYSKIT_KERN_OUT_PATH}/modules.install [提示]"
		return 1
	fi

	#-----------------------------------------------------------------------
	#更新到后备目录
	if [ -d ${SYSKIT_RTFS_RAWDIR_PATH}/lib/modules ]; then
		rm -rf ${SYSKIT_RTFS_RAWDIR_PATH}/lib/modules
	fi
	cp -rf ${SYSKIT_KERN_OUT_PATH}/modules.install/lib/modules  ${SYSKIT_RTFS_RAWDIR_PATH}/lib/
	echo "cp -rf ${SYSKIT_KERN_OUT_PATH}/modules.install/lib/modules"
	echo "       ${SYSKIT_RTFS_RAWDIR_PATH}/lib/"

	#-----------------------------------------------------------------------
	#更新到前置目录
	if [ -d ${SYSKIT_RTFS_OUT_PATH}/lib/modules ]; then
		rm -rf ${SYSKIT_RTFS_OUT_PATH}/lib/modules
	fi
	cp -rf ${SYSKIT_KERN_OUT_PATH}/modules.install/lib/modules  ${SYSKIT_RTFS_OUT_PATH}/lib/
	echo "cp -rf ${SYSKIT_KERN_OUT_PATH}/modules.install/lib/modules"
	echo "       ${SYSKIT_RTFS_OUT_PATH}/lib/"

	sdone "更新RTFS驱动目录: ... [完成]"
	snote "${inner_build_rtfs_fname}:${inner_build_rtfs_func}:--------------------"
	return 0
}

rtfs_dopack()
{
	local inner_build_rtfs_func=rtfs_pack
	snote "${inner_build_rtfs_fname}:${inner_build_rtfs_func}:+++++++++++++++++++++"
	#记录执行结果
	export pack_rtfs_ret=0
	
	snote "RTFS文件系统格式: ${OEMDEV_CUR_MTDFSTYP}"
	snote "RTFS前线打包目录: ${SYSKIT_RTFS_OUT_PATH}"
	snote "RTFS固件文件路径: ${IMGBINS_SUBDIR}/${SYSKIT_RTFS_BIN_NAME}"

	if [ ! -d ${SYSKIT_RTFS_OUT_PATH} ]; then
		sinfo "未检测到RTFS前线打包目录: ${SYSKIT_RTFS_OUT_PATH} [不存在]"
		export pack_rtfs_ret=1
		return 1
	fi
	sdone "已检测到RTFS前线打包目录: ${SYSKIT_RTFS_OUT_PATH}"

	#创建.bin输出目录
	if [ ! -d ${IMGBINS_SUBDIR} ]; then
		mkdir -p ${IMGBINS_SUBDIR}
	fi

	if [ "x${OEMDEV_CUR_MTDFSTYP}" = "xYAFS2" ]; then
		
		rtfs_ownroot
		
		mkyaffs2 --inband-tags -p 2048 ${SYSKIT_RTFS_OUT_PATH} 	${IMGBINS_SUBDIR}/${SYSKIT_RTFS_BIN_NAME}
		if [ $? -ne 0 ]; then
			export pack_rtfs_ret=3
		else
			export pack_rtfs_ret=0
		fi
		
		rtfs_ownuser

	elif [ "x${OEMDEV_CUR_MTDFSTYP}" = "xUBIFS" ]; then
		
		#UBI文件系统目录
		export UBI_DIN_FILE=${SYSKIT_RTFS_OUT_PATH}
        #UBI相关文件目录
		export UBI_CFG_ROOT=${MODSRC_ROOT}/ubifiles
        #UBI配置文件路径
		export UBI_CFG_FILE=${UBI_CFG_ROOT}/1.ubinize_rtfs.cfg
		#UBI依赖文件路径
		export UBI_REF_FILE=${UBI_CFG_ROOT}/2.ubinize_rtfs.ref
		#UBI烧写文件路径
		export UBI_BIN_FILE=${IMGBINS_SUBDIR}/${SYSKIT_RTFS_BIN_NAME}
		
		#UBI所需MTD分区容量
		UBI_MTD_SIZE=60

		UBI_MTD_BLOCK=$[ ${UBI_MTD_SIZE} * 1024 / 128  ]

		echo  "UBI检测当前MTD分区大小: ${UBI_MTD_SIZE} (MB)"
		
		rtfs_ownroot
		
		sudo mkfs.ubifs -r ${UBI_DIN_FILE} -F -m 2048 -e 126976 -c ${UBI_MTD_BLOCK}	-o ${UBI_REF_FILE} -v > /dev/null 2>&1
		if [ $? -ne 0 ]; then
	    	serro "UBI使用mkfs.ubifs创建依赖文件: ${UBI_REF_FILE} [错误]"
			exit 1
		fi
	    sdone "UBI使用mkfs.ubifs创建依赖文件: ${UBI_REF_FILE} [完成]"

		cd ${UBI_CFG_ROOT}
		ubinize -o  ${UBI_BIN_FILE} -m 2048 -p 128KiB -s 2048 ${UBI_CFG_FILE} -v 
		if [ $? -ne 0 ]; then
	    	serro "UBI使用ubinize创建烧写文件: ${UBI_BIN_FILE} [错误]"
			exit 1
		fi
	    sdone "UBI使用ubinize创建烧写文件: ${UBI_BIN_FILE} [完成]"
	    cd -> /dev/null
		
		rtfs_ownuser

	else
		export pack_rtfs_ret=99
	fi
	if [ ${pack_rtfs_ret} -eq 99 ]; then
		serro "rtfs文件系统格式: ${OEMDEV_CUR_MTDFSTYP} [不支持]"
		return 1
	elif [ ${pack_rtfs_ret} -ne 0 ]; then
		serro "打包rtfs固件：[失败]"
		return 1
	else
		sdone "打包rtfs固件：[完毕]"
	fi
	snote "${inner_build_rtfs_fname}:${inner_build_rtfs_func}:--------------------"
	return 0
}

#切换归属为root
rtfs_ownroot()
{
	#强制调整归属
	if [ -d ${SYSKIT_RTFS_OUT_PATH} ]; then
		cd ${SYSKIT_RTFS_OUT_PATH}
		sudo chown -R root:root ./*
		cd - > /dev/null
	fi
}

#切换归属为当前用户
rtfs_ownuser()
{
	#强制调整归属
	if [ -d ${SYSKIT_RTFS_OUT_PATH} ]; then
		cd ${SYSKIT_RTFS_OUT_PATH}
		sudo chown -R $(whoami):$(whoami) ./*
		cd - > /dev/null
	fi
}

mainproc() 
{

	rtfs_ownuser

	if [ "x$fun_code" = "x1" ]; then
		rtfs_koinstall
		rtfs_dopack
	else
		showusage $@
	fi
}

mainproc $@
